import axios from "../../axios/axiosInstance";
import { ElMessage } from "element-plus";
import * as notifications from "./notification";
// 增删改
export async function sendRequest(
  url: string,
  method: "get" | "post" | "put" | "delete",
  data?: any
): Promise<void> {
  try {
    const response = await axios({
      url,
      method,
      data,
    });
    if (response.data.code == 200 || response.data.code == 1) {
      notifications.showMessage(response.data.data, "success");
      return response.data;
    } else {
      notifications.showMessage(response.data.msg, "error");
      return response.data;
    }
  } catch (error) {
    notifications.showMessage("请求失败，请稍后重试", "error");
    throw error;
  }
}

export async function fetchData(
  url: string,
  method: "get" | "post" | "put" | "delete",
  data?: any
): Promise<any> {
  try {
    let response;

    if (method === "get") {
      response = await axios.get(url, { params: data });
    } else {
      response = await axios({
        url,
        method,
        data,
      });
    }
    return response.data;
  } catch (error) {
    // 处理错误
    notifications.showMessage("查询失败，请稍后重试", "error");
    throw error;
  }
}
